/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.icanci.loopstack.damc.admin.dal.mongo.mongo;

import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.damc.admin.dal.common.PageList;
import cn.icanci.loopstack.damc.admin.dal.mongo.daointerface.ClassScriptDAO;
import cn.icanci.loopstack.damc.admin.dal.mongo.daointerface.RegisterTableDAO;
import cn.icanci.loopstack.damc.admin.dal.mongo.dateobject.ClassScriptDO;

/**
 * @author icanci
 * @since 1.0 Created in 2025/05/17 07:24
 */
@Service("classScriptDAO")
public class MongoClassScriptDAO extends AbstractBaseDAO<ClassScriptDO> implements ClassScriptDAO {
    @Override
    public void insert(ClassScriptDO classScriptDO) {
        super.insert(classScriptDO);
        mongoTemplate.insert(classScriptDO, COLLECTION_NAME);
    }

    @Override
    public void update(ClassScriptDO classScriptDO) {
        super.update(classScriptDO);
        mongoTemplate.save(classScriptDO, COLLECTION_NAME);
    }

    /**
     * 查询文档所有记录
     *
     * @return 返回查询的结果
     */
    @Override
    public List<ClassScriptDO> queryAll() {
        Criteria criteria = Criteria.where(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    /**
     * 查询文档所有记录
     *
     * @param classScriptDO 请求参数
     * @param pageNum       pageNum
     * @param pageSize      pageSize
     * @return 返回查询的结果
     */
    @Override
    public PageList<ClassScriptDO> pageQuery(ClassScriptDO classScriptDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(RegisterTableDAO.RegisterTableColumn.ENV).is(DEFAULT_ENV);
        if (StringUtils.isNotBlank(classScriptDO.getAppCode())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(ClassScriptColumn.APP_CODE).regex("^.*" + classScriptDO.getAppCode() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(classScriptDO.getClazzName())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(ClassScriptColumn.CLAZZ_NAME).regex("^.*" + classScriptDO.getClazzName() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(classScriptDO.getMethodName())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(ClassScriptColumn.METHOD_NAME).regex("^.*" + classScriptDO.getMethodName() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(classScriptDO.getDescriptor())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(ClassScriptColumn.DESCRIPTOR).regex("^.*" + classScriptDO.getDescriptor() + ".*$", "i");
        }

        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, ClassScriptColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    /**
     * 根据 _id 查询一条信息
     *
     * @param _id _id
     * @return 返回查询的结果
     */
    @Override
    public ClassScriptDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(ClassScriptColumn._ID).is(_id);
        criteria.and(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public void deleteById(String id) {
        Criteria criteria = Criteria.where(ClassScriptColumn._ID).is(id);
        criteria.and(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        mongoTemplate.remove(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Collection<ClassScriptDO> queryEnableByAppCodeAndClazzName(String appCode, String clazzName) {
        Criteria criteria = Criteria.where(ClassScriptColumn.APP_CODE).is(appCode);
        criteria.and(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        criteria.and(ClassScriptColumn.DELETED).is(false);
        criteria.and(ClassScriptColumn.CLAZZ_NAME).is(clazzName);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public ClassScriptDO queryUniqueOne(ClassScriptDO classScriptDO) {
        Criteria criteria = Criteria.where(ClassScriptColumn.APP_CODE).is(classScriptDO.getAppCode());
        criteria.and(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        criteria.and(ClassScriptColumn.CLAZZ_NAME).is(classScriptDO.getClazzName());
        criteria.and(ClassScriptColumn.METHOD_NAME).is(classScriptDO.getMethodName());
        criteria.and(ClassScriptColumn.DESCRIPTOR).is(classScriptDO.getDescriptor());
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Collection<ClassScriptDO> queryEnableByAppCode(String appCode) {
        Criteria criteria = Criteria.where(ClassScriptColumn.APP_CODE).is(appCode);
        criteria.and(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        criteria.and(ClassScriptColumn.DELETED).is(false);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return ClassScriptColumn.class;
    }

}
